﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace IF.Utils
{
    public class AddressClass
    {
        public static DataTable dt_Address;

        /// <summary>
        /// <para>加载资源</para>
        /// </summary>
        /// <param name="path"></param>
        public static void LoadResource(string path, string tableName)
        {
            if (null == dt_Address || dt_Address.Rows.Count == 0)
            {
                dt_Address = NOPIHelper.RenderDataTableFromExcel(path, tableName, 0);
            }
        }

        public static JObject GetAddress(string param_Address)
        {
            string addressinfo = string.Empty;
            param_Address = param_Address.Trim();
            //解析 省

            DataTable temp_DataTable = GetNewTable("100000");

            string sheng = string.Empty;
            string shi = string.Empty;
            string qu = string.Empty;
            string shengyuan = string.Empty;
            string shiyuan = string.Empty;
            string quyuan = string.Empty;

            string dizhi = string.Empty;

            string shengID = string.Empty;
            string shiID = string.Empty;
            string quID = string.Empty;

            foreach (DataRow item in temp_DataTable.Rows)
            {
                if (!Regex.IsMatch(param_Address, "^(" + item["简称"].ToString() + "(省){0,1})"))
                {
                    continue;
                }
                shengID = item["城市代码"].ToString();
                shengyuan = Regex.Match(param_Address, "^(" + item["简称"].ToString() + "(省){0,1})").Groups[1].Value;
                sheng = item["城市"].ToString();
                continue;
            }
            sheng = string.IsNullOrEmpty(sheng) ? "无" : sheng;
            param_Address = param_Address.Remove(0, shengyuan.Length);
            param_Address = param_Address.Trim();
            temp_DataTable = GetNewTable(shengID);
            foreach (DataRow item in temp_DataTable.Rows)
            {
                if (!Regex.IsMatch(param_Address, "^(" + item["简称"].ToString() + "(市){0,1})"))
                {
                    continue;
                }

                shiID = item["城市代码"].ToString();
                shiyuan = Regex.Match(param_Address, "^(" + item["简称"].ToString() + "(市){0,1})").Groups[1].Value;
                shi = item["城市"].ToString();
                continue;
            }
            shi = string.IsNullOrEmpty(shi) ? "无" : shi;
            param_Address = param_Address.Remove(0, shiyuan.Length);
            param_Address = param_Address.Trim();
            temp_DataTable = GetNewTable(shiID);
            foreach (DataRow item in temp_DataTable.Rows)
            {
                if (!Regex.IsMatch(param_Address, "^(" + item["简称"].ToString() + "[区/县/市]{0,1})"))
                {
                    continue;
                }
                quID = item["城市代码"].ToString();
                quyuan = Regex.Match(param_Address, "^(" + item["简称"].ToString() + "[区/县/市]{0,1})").Groups[1].Value;
                qu = item["城市"].ToString();
                continue;
            }
            qu = string.IsNullOrEmpty(qu) ? "无" : qu;
            param_Address = param_Address.Remove(0, quyuan.Length);
            param_Address = param_Address.Trim();
            dizhi = param_Address;
            JObject jobject = new JObject();
            jobject.Add("province", sheng);
            jobject.Add("city", shi);
            jobject.Add("district", qu);
            jobject.Add("address", dizhi);
            return jobject;
        }

        //获取新数据
        private static DataTable GetNewTable(string pCode)
        {
            //解析 省
            DataView dv = new DataView(dt_Address);
            dv.RowFilter = "上级代码='" + pCode + "'";
            return dv.ToTable();
        }
    }
}
